<template>
	<view class="page">
		<view class="card" v-for="(item,index) in list" v-if="type == 1||type == 3||type == 4 " :key="item.id">
			<view class="ful">
				<view class="ful_left">
					{{type == 1?'报价':(type == 3?"发票":'成交价')}}金额
				</view>
				<view class="ful_right">
					￥{{item.money||0}}
				</view>
			</view>
			<view class="ful border">
				<view class="ful_left">
					{{type == 1?'创建':(type == 3?"发票":'提交')}}时间
				</view>
				<view class="ful_right">
					{{item.createtime||'-'}}
				</view>
			</view>
			<view class="dis2" v-if="type!=4">
				<image v-for="(img, k) in item.imgArr" :key="k" :src="img" class="row-value"
					@click="preview(k,item.imgArr)" />
			</view>
			<view class="dis2" v-if="type==4">
				<view class="ful_left" style="width: 52%;">
					成交价说明
				</view>
				<view class="ful_right">
					{{item.remarks||''}}
				</view>
			</view>
		</view>
		<view class="card" v-for="(item,index) in list" v-if="type == 2" :key="item.id">
			<view class="ful">
				<view class="ful_left">编号</view>
				<view class="ful_right">{{item.warranty.workID||'-'}}</view>
			</view>
			<view class="ful border">
				<view class="ful_left">上传合同时间</view>
				<view class="ful_right">{{item.create_time||'-'}}</view>
			</view>
			<view class="dis" v-if="item.url" @tap="reviewPdf(list,index)">
				<view class="btn">下载</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: null,
				list: [],
				type: 0,
				// page: 1,
				// last_page: null,
			}
		},
		onLoad(e) {
			this.id = e.id
			this.type = e.type
			uni.setNavigationBarTitle({
				title: e.type == 1 ? '报价列表' : (e.type == 2 ? '合同列表' : (e.type == 3 ?'发票列表':"成交价详情"))
			})
		},
		onShow() {
			if (this.id) {
				this.list = []
				this.getList(this.type)
			}
		},
		methods: {
			// 预览图片
			preview(current, urls) {
				uni.previewImage({
					current,
					urls,
				});
			},
			reviewPdf(item, index) {
				setTimeout(() => {
					uni.showLoading({
						title: '加载中',
						duration: 1500
					});
				}, 100)
				setTimeout(() => {
					uni.getSystemInfo({
						success: res => {
							let that = this
							wx.downloadFile({
								url: item[index].url,
								filePath: '',
								success: res => {
									const filePath = res.filePath ||
										res.tempFilePath
									console.log(filePath)
									wx.openDocument({
										filePath: filePath,
										showMenu: true, // 显示右上角菜单
										fileType: 'pdf',
										success: () => {
											console.log(
												'打开文档成功'
											)
											uni.hideLoading();
										},
										fail: err => {
											console.log(
												`打开文档失败`,
												err)
										}
									})
								},
								fail: err => {
									console.log(`下载文档失败`, err)
								}
							})
						}
					});
				}, 0)
			},
			getList(e) {
				if(e!=4){
					let url = e == 1 ? '/warranty/offer_log' : (e == 2 ? '/warranty/contract_list' : '/warranty/billing_log')
					this.$https.post(url, {
						warranty_id: this.id,
					}).then(res => {
						// this.last_page = res.data.last_page
						this.list = this.list.concat(res.data).map(item => {
							return {
								...item,
								imgArr: item.image ? item.image.split(',') : ''
							}
						})
					}).catch(err => {
						console.log(err);
					})
				}else{
					this.$https.post('/hospital/upfinal_log', {
						warranty_id: this.id,
					}).then(res => {
						this.list = this.list.concat(res.data)
					}).catch(err => {
						console.log(err);
					})
				}
				
			},
		},
		// onReachBottom() {
		// 	if (this.page != this.last_page) {
		// 		this.page = this.page + 1
		// 		this.getList()
		// 	}
		// }
	}
</script>

<style lang="scss" scoped>
	.dis {
		display: flex;
		justify-content: flex-end;
		padding: 10rpx;
	}

	.dis2 {
		display: flex;
		justify-content: flex-start;

		.row-value {
			width: 140rpx;
			height: 140rpx;
			margin: 20rpx;
		}
	}

	.btn {
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #1A66FF;
		padding: 6rpx 32rpx;
		border-radius: 32rpx;
		opacity: 1;
		border: 2px solid #1A66FF;
	}

	.border {
		border-bottom: 2rpx solid #f7f8fb;
	}

	.page {
		background-color: #f7f8fb;
		min-height: 100vh;
	}

	.ful {
		display: flex;
		justify-content: space-between;
	}

	.ful_left {
		color: #303030;
		padding: 20rpx;
	}

	.ful_right {
		padding: 20rpx;
	}

	.card {
		font-size: smaller;
		color: #999999;

		margin: 30rpx;
		background-color: #fff;
		padding: 10rpx;
		border-radius: 10rpx;
		box-shadow: 10rpx 10rpx 10rpx 10rpx;
	}
</style>